<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\response\Json;
use app\admin\model\HelpCategory as HelpCategoryModel;
/**
 * 帮助文档分类
 * Class HelpCategory
 * @package app\admin\controller
 */
class HelpCategory extends Controller
{
    /**
     * 列表
     * @return Json
     */
    public function lists(): Json
    {
        $model = new HelpCategoryModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(['items'=>$list]);
    }
    /**
     * 树
     * @return Json
     */
    public function trees(): Json
    {
        $model = new HelpCategoryModel;
        $list = $model->getList(['status'=>-1])->toArray();
        $trees = $model->getTreeData($list);
        return $this->renderSuccess($trees);
    }
    /**
     * 设置状态
     * @return Json
     */
    public function setstatus(): Json
    {
        $params = $this->postData();
        $model = HelpCategoryModel::detail($params['id']);
        if ($model->edit(['status' => $params['status']])) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 添加
     * @return Json
     */
    public function add(): Json
    {
        $model = new HelpCategoryModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     * @param int $id
     * @return Json
     */
    public function edit(int $id): Json
    {
        $model = HelpCategoryModel::detail($id);
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除分类
     * @param int $id
     * @return Json
     */
    public function delete(int $id): Json
    {
        $model = HelpCategoryModel::detail($id);
        if (!$model->delete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}
